package com.neuronbit.businessflow.intellij.psi.reference;

import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * The type Simple reference.
 */
public final class SimpleReference extends PsiReferenceBase<PsiElement> implements PsiPolyVariantReference {
    private final PsiElement sourceElement;

    /**
     * Instantiates a new Simple reference.
     *
     * @param element       the element
     * @param sourceElement the source element
     * @param textRange     the text range
     */
    public SimpleReference(@NotNull PsiElement element, @Nullable PsiElement sourceElement, TextRange textRange) {
        super(element, textRange);
        this.sourceElement = sourceElement;
    }

    @Override
    public ResolveResult @NotNull [] multiResolve(boolean incompleteCode) {
        List<ResolveResult> results = new ArrayList<>();
        results.add(new PsiElementResolveResult(this.sourceElement));
        return results.toArray(new ResolveResult[0]);
    }

    @Nullable
    @Override
    public PsiElement resolve() {
        ResolveResult[] resolveResults = multiResolve(false);
        return resolveResults.length == 1 ? resolveResults[0].getElement() : null;
    }

    @Override
    public Object @NotNull [] getVariants() {
        Project project = myElement.getProject();
//        List<SimpleProperty> properties = SimpleUtil.findProperties(project);
        List<LookupElement> variants = new ArrayList<>();
//        for (final SimpleProperty property : properties) {
//            if (property.getKey() != null && property.getKey().length() > 0) {
//                variants.add(LookupElementBuilder
//                        .create(property).withIcon(SimpleIcons.FILE)
//                        .withTypeText(property.getContainingFile().getName())
//                );
//            }
//        }
        return variants.toArray();
    }

}
